import es

from wcs.libs.players import players
from wcs.libs.items import items

itemName = 'Orb of Frost'
item = items.addItem(itemName)
item.setGroup("attack")
item.setCost(3500)
item.setDescription("You have a small cahnce of freezing an enemy")
item.aliveToUse = True
item.setMaxCount(1)
        
def player_hurt(event_var):
    """
    Executed when a player is damaged. If the attacker has this item active,
    do the random number chance generation. If the chance succeeds, then freeze
    the player.
    """
    attacker = int(event_var["attacker"]) if event_var["attacker"] else 0
    userid = event_var["userid"]
    if not attacker:
        return
    if event_var["es_userteam"] == event_var["es_attackerteam"]:
        return
    player = players[attacker]
    if not player.getItem(itemName).count:
        return
    player = players[userid]
    if random.randint(1, 100) > 5:
        return
    player.freeze(3)
    text = ("#green%s #default- #lightgreenYou have frozen #green%s " +
            "#lightgreenfor #green3#lightgreenseconds.")
    es.tell(attacker, "#multi", text % (itemName, event_var["es_username"]))
    text = ("#green%s #default- #lightgreenYou have been frozen by #green%s " +
            "#lightgreenfor #green3#lightgreenseconds.")
    es.tell(attacker, "#multi", text % (itemName, event_var["es_attackername"]))